package com.ljx.funchat.websocket.service.adatper;

import com.ljx.funchat.chat.domain.vo.resp.ChatMessageResp;
import com.ljx.funchat.user.domain.entity.User;
import com.ljx.funchat.user.domain.enums.ChatActiveStatusEnum;
import com.ljx.funchat.websocket.domain.vo.resp.*;
import com.ljx.funchat.websocket.domain.enums.WSRespTypeEnum;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;

public class WebSocketAdapter {

    public static WSBaseResp<?> buildResp(User user, String token) {
        WSBaseResp<WSLoginSuccess> wsBaseResp = new WSBaseResp<>();
        WSLoginSuccess wsLoginSuccess = WSLoginSuccess.builder()
                .avatar(user.getAvatar())
                .name(user.getName())
                .token(token)
                .uid(user.getId())
                .build();
        wsBaseResp.setData(wsLoginSuccess);
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SUCCESS.getType());
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildResp(WxMpQrCodeTicket wxMpQrCodeTicket) {
        WSBaseResp<WSLoginUrl> wsBaseResp = new WSBaseResp<>();
        WSLoginUrl wsLoginUrl = WSLoginUrl.builder()
                .loginUrl(wxMpQrCodeTicket.getUrl())
                .build();
        wsBaseResp.setData(wsLoginUrl);
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_URL.getType());
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildWaitAuthorizeResp() {
        WSBaseResp<Object> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setData(null);
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildInvalidTokenResp() {
        WSBaseResp<Object> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setData(null);
        wsBaseResp.setType(WSRespTypeEnum.INVALIDATE_TOKEN.getType());
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildMsgSend(ChatMessageResp wsMessage) {
        WSBaseResp<Object> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setData(wsMessage);
        wsBaseResp.setType(WSRespTypeEnum.MESSAGE.getType());
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildApplyResp(WSApplyResp wsApplyResp) {
        WSBaseResp<Object> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setData(wsApplyResp);
        wsBaseResp.setType(WSRespTypeEnum.APPLY.getType());
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildUserOffLineResp(User user) {
        WSBaseResp<Object> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setData(new WSOnlineResp(ChatActiveStatusEnum.OFFLINE.getStatus(), user.getId()));
        wsBaseResp.setType(WSRespTypeEnum.ONLINE_OFFLINE_NOTIFY.getType());
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildUserOnLineResp(User user) {
        WSBaseResp<Object> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setData(new WSOnlineResp(ChatActiveStatusEnum.ONLINE.getStatus(), user.getId()));
        wsBaseResp.setType(WSRespTypeEnum.ONLINE_OFFLINE_NOTIFY.getType());
        return wsBaseResp;
    }
}
